package com.house.framework.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Description: CloseableHttpClient http工具类
 * @Author: ArvinWoo
 * @Date: 2022/8/10 14:06
 **/
public class HttpClientUnit {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(HttpClientUnit.class);

    /**
     * @Description: post 请求
     * @Author: ArvinWoo
     * @Date: 2022/8/10 14:07
     * @MethodName: doPost
     * @Param: [param, url]
     * @Return: java.lang.String
     **/
    public static String doPost(Map<String, Object> param, String url) {
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(300 * 1000)
                    .setConnectTimeout(300 * 1000)
                    .build();
            HttpPost post = new HttpPost(url);
            post.setConfig(requestConfig);
            post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            if (param != null) {
                List<NameValuePair> form = new ArrayList<NameValuePair>();
                for (String name : param.keySet()) {
                    form.add(new BasicNameValuePair(name, (String) param.get(name)));
                }
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form, "utf-8");
                post.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(post);
            String content = EntityUtils.toString(response.getEntity(), "utf-8");
            logger.info("调用" + url + "接口\n请求参数:" + param + "\n请求结果:" + content);
            return content;
        } catch (SocketTimeoutException e) {
            logger.error("调用" + url + "接口超时");
            return null;
        } catch (Exception e) {
            logger.error("调用" + url + "接口失败");
            return null;
        }
    }

    /**
     * @Description: get 请求
     * @Author: ArvinWoo
     * @Date: 2022/8/10 14:08
     * @MethodName: doGet
     * @Param: [url]
     * @Return: org.springframework.http.HttpEntity
     **/
    public static HttpEntity doGet(String url) {
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(300 * 1000)
                    .setConnectTimeout(300 * 1000)
                    .build();
            HttpGet get = new HttpGet(url);
            get.setConfig(requestConfig);
            get.setHeader("Content-Type", "application/json;charset=utf-8");
            HttpResponse response = httpClient.execute(get);
            return response.getEntity();
        } catch (SocketTimeoutException e) {
            logger.error("调用" + url + "接口超时");
            return null;
        } catch (Exception e) {
            logger.error("调用" + url + "接口失败");
            return null;
        }
    }

    public static HttpEntity doGet(String url, int  timeout) {
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(timeout)
                    .setConnectTimeout(timeout)
                    .build();
            HttpGet get = new HttpGet(url);
            get.setConfig(requestConfig);
            get.setHeader("Content-Type", "application/json;charset=utf-8");
            HttpResponse response = httpClient.execute(get);
            return response.getEntity();
        } catch (SocketTimeoutException e) {
            logger.error("调用" + url + "接口超时");
            return null;
        } catch (Exception e) {
            logger.error("调用" + url + "接口失败");
            return null;
        }
    }

    /**
     * @Description: 测试类
     * @Author: ArvinWoo
     * @Date: 2022/8/10 14:08
     * @MethodName: main
     * @Param: [args]
     * @Return: void
     **/
    public static void main(String[] args) throws IOException {
//        String url = "http://60.208.23.61:8994/ziBo/selectHISTORY";
//        HttpEntity entity = doGet(url);
//        logger.info(url);
//        logger.info(EntityUtils.toString(entity, "utf-8"));


    }

}
